package com.cy.train.member.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cy.train.common.context.LoginMemberContext;
import com.cy.train.common.resp.CommonResp;
import com.cy.train.common.resp.PageResp;
import com.cy.train.member.domain.Passenger;
import com.cy.train.member.req.PassengerQueryReq;
import com.cy.train.member.req.PassengerSaveReq;
import com.cy.train.member.resp.PassengerQueryResp;
import com.cy.train.member.service.PassengerService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/passenger")
public class PassengerController {
    @Autowired
    private PassengerService passengerService;

    @PostMapping("/save")
    public CommonResp<Object> register(@Valid @RequestBody PassengerSaveReq req) {
        passengerService.save(req);
        return new CommonResp<>();
    }

    @GetMapping("/query-list")
    public CommonResp<PageResp> register(@Valid PassengerQueryReq req) {
        if(ObjectUtil.isNull(req)) {
            req = new PassengerQueryReq();
            req.setMemberId(LoginMemberContext.getId());
        }
        PageResp<PassengerQueryResp> resp = passengerService.queryList(req);
        return new CommonResp<>(resp);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp<Object> delete(@PathVariable Long id) {
        passengerService.delete(id);
        return new CommonResp<>();
    }

    @GetMapping("/query-mine")
    public CommonResp<List<Passenger>> queryMine() {
        List<Passenger> passengerList = passengerService.queryAll();
        return new CommonResp<>(passengerList);
    }
}